package com.dm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dm.entity.CompanyGroupInfoDO;
import com.dm.entity.SysUserDO;
import com.dm.query.QueryCompanyGroupInfo;
import com.dm.query.QueryCompanyInfo;
import com.dm.query.QuerySysUser;
import com.dm.service.CompanyGroupInfoService;
import com.dm.service.CompanyInfoService;
import com.dm.service.SysUserService;
import com.dm.utils.RespsonseResult;



@Controller
@RequestMapping("group")
public class CompanyGroupController {
	
	private static Logger logger = LoggerFactory.getLogger(SysUserController.class);

	@Autowired
	CompanyGroupInfoService companyGroupInfoService ;
	
	@Autowired
	CompanyInfoService companyInfoService ;
	
	@Autowired
	SysUserService sysUserService ;
	
	
	@RequestMapping("selectgroup")
	public @ResponseBody Object selectGroup(HttpServletRequest request,QueryCompanyGroupInfo queryCompanyGroupInfo){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		 logger.info("企业分组查询:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 //判断用户权限
			 if(userInfo.getType()>2){
				 result.setMsg("权限不足。企业用户无法查询");
				 return JSON.toJSON(result);    
			 }
			 if(userInfo.getType() != 0){
				 queryCompanyGroupInfo.setGroupCode(userInfo.getCompanyInfo());
			 }
			 queryCompanyGroupInfo.setFuzzyName(queryCompanyGroupInfo.getGroupName());
			 queryCompanyGroupInfo.setGroupName(null);
			 List< CompanyGroupInfoDO > data = companyGroupInfoService.selectCompanyGroupInfoList(queryCompanyGroupInfo);
			 for(CompanyGroupInfoDO index : data ){
				 QueryCompanyInfo queryCompanyInfo = new QueryCompanyInfo();
				 queryCompanyInfo.setGroupCode(index.getGroupCode());
				 int companyCount = companyInfoService.countCompanyInfoList(queryCompanyInfo);
				 QuerySysUser querySysUser = new QuerySysUser();
				 querySysUser.setCompanyInfo(index.getGroupCode());
				 querySysUser.setType(2l);
				 int yw = sysUserService.countSysUserList(querySysUser);
				 querySysUser.setType(1l);
				 int yw1 = sysUserService.countSysUserList(querySysUser);
				 index.setConSum(companyCount);
				 int sum = yw+yw1 ; 
				 index.setUserSum(sum);
			 }
			 result.setData(data);
			 result.setMsg("查询成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("查询出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	@RequestMapping("addgroup")
	public @ResponseBody Object addGroup(HttpServletRequest request,CompanyGroupInfoDO companyGroupInfoDO){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		logger.info("企业分组录入:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 result.setMsg("权限不足。只有超级管理员可以录入");
				 return JSON.toJSON(result);    
			 }
			 if(StringUtils.isBlank(companyGroupInfoDO.getGroupCode())
					 ||StringUtils.isBlank(companyGroupInfoDO.getGroupName())){
				 result.setMsg("参数不能为空");
				 return JSON.toJSON(result);
			 }
//			 QueryCompanyGroupInfo nameInfo = new QueryCompanyGroupInfo();
//			 nameInfo.setGroupName(companyGroupInfoDO.getGroupName());
//			 if(companyGroupInfoService.selectOneCompanyGroupInfo(nameInfo) != null){
//				 result.setMsg("该分组已存在");
//				 return JSON.toJSON(result);
//			 }
			 
			 QueryCompanyGroupInfo codeInfo = new QueryCompanyGroupInfo();
			 codeInfo.setGroupCode(companyGroupInfoDO.getGroupCode());
			 if(companyGroupInfoService.selectOneCompanyGroupInfo(codeInfo) != null){
				 result.setMsg("该分组代码已存在");
				 return JSON.toJSON(result);
			 }
			 companyGroupInfoService.insertCompanyGroupInfo(companyGroupInfoDO);
			 result.setMsg("录入成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("录入出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	@RequestMapping("upgroup")
	public @ResponseBody Object upGroup(HttpServletRequest request,CompanyGroupInfoDO companyGroupInfoDO){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		logger.info("企业分组修改:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 result.setMsg("权限不足。只有超级管理员可以修改");
				 return JSON.toJSON(result);    
			 }
			 
			 if(StringUtils.isBlank(companyGroupInfoDO.getGroupCode())
					 ||StringUtils.isBlank(companyGroupInfoDO.getGroupName())){
				 result.setMsg("参数不能为空");
				 return JSON.toJSON(result);
			 }
			 
			 
			 QueryCompanyGroupInfo qInfo = new QueryCompanyGroupInfo();
			 qInfo.setGroupName(companyGroupInfoDO.getGroupName());
			 qInfo.setGroupCodeNo(companyGroupInfoDO.getGroupCode());
			 if(companyGroupInfoService.selectOneCompanyGroupInfo(qInfo)!=null){
				 result.setMsg("该分组已存在");
				 return JSON.toJSON(result);
			 }
			 companyGroupInfoService.updateCompanyGroupInfoByGroupId(companyGroupInfoDO);
			 result.setMsg("修改成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("修改出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	@RequestMapping("delgroup")
	public @ResponseBody Object delgroup(HttpServletRequest request,CompanyGroupInfoDO companyGroupInfoDO){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		logger.info("企业分组删除:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 result.setMsg("权限不足。只有超级管理员可以删除");
				 return JSON.toJSON(result);    
			 }
			 
			 if(StringUtils.isBlank(companyGroupInfoDO.getGroupCode())){
				 result.setMsg("参数不能为空");
				 return JSON.toJSON(result);
			 }
			 
			 QueryCompanyInfo queryCompanyInfo = new QueryCompanyInfo();
			 queryCompanyInfo.setGroupCode(companyGroupInfoDO.getGroupCode());
			 if(companyInfoService.countCompanyInfoList(queryCompanyInfo)>0){
				 result.setMsg("该分组下有企业信息，无法删除");
				 return JSON.toJSON(result);
			 }
			 companyGroupInfoService.deleteCompanyGroupInfoByGroupId(companyGroupInfoDO);
			 sysUserService.deleteSysUserCompanyInfo(companyGroupInfoDO.getGroupCode());

			 result.setMsg("删除成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("删除出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
}
